﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataControl;
using System.Data;
using System.Text;

namespace ATW.MT.BUS
{
    public class bus_T_InformationCategory
    {
        DataAccess da = null;

        public bus_T_InformationCategory()
        {
            da = new DataAccess();
        }

        #region 方法
        /// <summary>
        /// 新增子项
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <param name="level_class"></param>
        /// <returns></returns>
        public int Add(string strName, string strDesc, string strUrl)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_InformationCategory(ticName,ticDesc,ticPictureUrl,ticRowStatus) values('{0}','{1}','{2}','1')",
                strName, strDesc, strUrl);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 新增子项
        /// </summary>
        /// <param name="name"></param>
        /// <param name="desc"></param>
        /// <param name="url"></param>
        /// <param name="level_c"></param>
        /// <param name="level_d"></param>
        /// <returns></returns>
        public int Add(string name, string desc, string url, string level_c, string level_d)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_InformationCategory(ticName,ticDesc,ticPictureUrl,ticRowStatus,[level_c_supply],[level_d_supply])
                        values('{0}','{1}','{2}','1','{3}','{4}')", name, desc, url, level_c, level_d);
            return da.ExcuteNoQuery(sql.ToString());
        }

        /// <summary>
        /// 修改子项
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <param name="level_class"></param>
        /// <returns></returns>
        public int Modify(string strId, string strName, string strDesc, string strUrl)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_InformationCategory set ticName='{0}',ticDesc='{1}',ticPictureUrl='{2}',UpdatedDate=getdate() where TICID={3}",
                strName, strDesc, strUrl, strId);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 修改子项
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="name"></param>
        /// <param name="desc"></param>
        /// <param name="url"></param>
        /// <param name="level_c"></param>
        /// <param name="level_d"></param>
        /// <returns></returns>
        public int Modify(string tid, string name, string desc, string url, string level_c, string level_d)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_InformationCategory set ticName='{0}',ticDesc='{1}',ticPictureUrl='{2}',UpdatedDate=getdate(),
                [level_c_supply]='{4}',[level_d_supply]='{5}' where TICID={3}", name, desc, url, tid, level_c, level_d);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 用户分页查询
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="status">状态，1正常，2删除</param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable UserPagerQuery(string strName, string strStatus, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_InformationCategory where 1=1");
            if (strName != "")
            {
                sql.Append(" and ticName like '%" + strName + "%'");
            }
            if (strStatus != "")
            {
                sql.Append(" and ticRowStatus='" + strStatus + "'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "TICID");
        }

        /// <summary>
        /// 用户分页查询
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="status">状态，1正常，2删除</param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable UserPagerQueryEx(string strName, string strStatus, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select t.*,isnull(s.num,'0') num from T_InformationCategory t left join 
                                    (select COUNT(*) num,ticid from D_Item where diRowStatus<>'2' group by ticid)s
                                    on t.TICID=s.ticid where 1=1");
            if (strName != "")
            {
                sql.Append(" and t.ticName like '%" + strName + "%'");
            }
            if (strStatus != "")
            {
                sql.Append(" and t.ticRowStatus='" + strStatus + "'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "TICID");
        }
        /// <summary>
        /// 类别分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable CatePagerQuery(string name, string status, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select top 100 percent t.TICID,
	                           t.ticName,
	                           t.ticDesc,
	                           t.ticPictureUrl,
	                           ISNULL(au.tauName,'无') c_name,
	                           ISNULL(au2.tauname,'无') d_name,
                               t.CreatedDate,
	                           t.ticRowStatus,
	                           isnull(s.num,'0') num from T_InformationCategory t 
	                           left join 
			                        (select COUNT(*) num,ticid from D_Item where diRowStatus<>'2' group by ticid)s on t.TICID=s.ticid 
	                           left join T_AppUser au on au.TAUID=t.level_c_supply
	                           left join T_AppUser au2 on au2.TAUID=t.level_d_supply
	                           where 1=1");
            if (!string.IsNullOrEmpty(name))
            {
                sql.Append(" and t.ticname like '%" + name + "%'");
            }
            if (!string.IsNullOrEmpty(status))
            {
                sql.Append(" and t.ticRowStatus ='" + status + "'");
            }
            sql.Append(" order by t.CreatedDate desc");
            return da.PageSize(sql.ToString(), page, pagesize, "TICID", "CreatedDate desc");
        }
        /// <summary>
        /// 改变用户状态
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="status">1正常 2封禁</param>
        /// <returns></returns>
        public int UserChangeStatus(string strId, string strStatus)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_InformationCategory set ticRowStatus='" + strStatus + "' where TICID='" + strId + "'");
            return da.ExcuteNoQuery(sql.ToString());
        }


        public DataTable SelectItem(string strId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select t.TICID,
	                                   t.ticName,
	                                   t.ticDesc,
	                                   t.ticPictureUrl,
	                                   t.level_c_supply,
	                                   t.level_d_supply,
	                                   t.ticRowStatus
	                                   from T_InformationCategory t 
             where TICID='" + strId + "'");
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 获取授权的用户列表
        /// </summary>
        /// <param name="ticid"></param>
        /// <returns></returns>
        public DataTable GetAuthedUser(string ticid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select u.TAUID,u.tauName
                                 from T_InformationCategory info
                                left join T_AppUserAuthorised ua on ua.tauaTicId=info.TICID
                                left join T_AppUser u on u.TAUID=ua.tauaTauId
                                where 1=1 and u.tauRowStatus<>'2' and info.TICID='{0}'", ticid);
            return da.ExecuteDataTable(sql.ToString());
        }
        #endregion
    }
}